package com.xm.thread.wait2notify;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 方案4：使用Semaphore（资源配额控制）
 */
@Slf4j
public class WaitToNotify05 {
    Semaphore semaphore = new Semaphore(10);

    protected final AtomicInteger atomicInt = new AtomicInteger(0);
    Runnable countTask = () -> {
        try {
            semaphore.acquire();// 获取许可
            atomicInt.incrementAndGet();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            semaphore.release();// 释放许可
        }
    };
}
